package com.fm.demo.jpush

import android.content.Context
import android.content.Intent
import android.os.Bundle
import cn.jpush.android.api.*
import cn.jpush.android.service.JPushMessageReceiver
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.fm.demo.MainActivity
import com.fm.demo.utils.CommonUtils

class PushMessageReceiver : JPushMessageReceiver() {

    companion object {
        private const val TAG = "PushMessageReceiver"
    }

    override fun onMessage(context: Context, customMessage: CustomMessage) {
        LogUtils.eTag(TAG, "[onMessage] $customMessage")
        CommonUtils.showToastShotMain("收到自定义消息")
    }

    /**
     * 用户打开通知
     * @param context 上下文
     * @param message 消息
     * @Des 用作定义跳转目的
     */
    override fun onNotifyMessageOpened(context: Context, message: NotificationMessage) {
        LogUtils.eTag(TAG, "[onNotifyMessageOpened] $message")
        try {
            //打开自定义的Activity
            val intent = Intent()
            val bundle = Bundle()
            val data: JsonObject = Gson().fromJson(message.notificationExtras, JsonObject::class.java)
            //跳转类型
            val intentType = if (data.has("intentType")) {
                data.get("intentType").asString
            } else {
                ""
            }
            when (intentType) {
                "1" -> {
                    intent.setClass(context, JPushActivity::class.java)
                }
                else -> {
                    intent.setClass(context, MainActivity::class.java)
                    bundle.putString("className", JPushActivity::class.java.name)
                }
            }
            //跳转目标页面，二次操作类型
            val nextStepType = if (data.has("nextStepType")) {
                data.get("nextStepType").asString
            } else {
                "9999"
            }
            bundle.putString("nextStepType", nextStepType)
            bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE, message.notificationTitle)
            bundle.putString(JPushInterface.EXTRA_ALERT, message.notificationContent)
            intent.putExtras(bundle)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
            context.startActivity(intent)
        } catch (throwable: Throwable) {
            throwable.printStackTrace()
        }

    }

    /**
     * 消息已送达
     * @param context 上下文
     * @param message 消息内容
     */
    override fun onNotifyMessageArrived(context: Context, message: NotificationMessage) {
        LogUtils.eTag(TAG, "[onNotifyMessageArrived] $message")
        CommonUtils.showToastShotMain("收到消息")
    }

    override fun onNotifyMessageDismiss(context: Context, message: NotificationMessage) {
        LogUtils.eTag(TAG, "[onNotifyMessageDismiss] $message")
        CommonUtils.showToastShotMain("消息删除")
    }

    /**
     * 初始化注册完成回调
     * @param context 上下文
     * @param registrationId 注册id
     */
    override fun onRegister(context: Context, registrationId: String) {
        LogUtils.eTag(TAG, "[onRegister] $registrationId")
        CommonUtils.showToastShotMain("注册成功")
    }


    override fun onConnected(context: Context, isConnected: Boolean) {
        LogUtils.eTag(TAG, "[onConnected] $isConnected")
        CommonUtils.showToastShotMain("连接成功")
    }

    override fun onCommandResult(context: Context, cmdMessage: CmdMessage) {
        LogUtils.eTag(TAG, "[onCommandResult] $cmdMessage")
    }

    /**
     * tag 增删查改的操作会在此方法中回调结果
     *
     * @param context      上下文
     * @param jPushMessage 回调结果
     */
    override fun onTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance.onTagOperatorResult(context, jPushMessage)
        super.onTagOperatorResult(context, jPushMessage)
    }

    /**
     * 查询某个 tag 与当前用户的绑定状态的操作会在此方法中回调结果
     *
     * @param context      上下文
     * @param jPushMessage 回调结果
     */
    override fun onCheckTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance.onCheckTagOperatorResult(context, jPushMessage)
        super.onCheckTagOperatorResult(context, jPushMessage)
    }

    /**
     * alias 相关的操作会在此方法中回调结果
     *
     * @param context
     * @param jPushMessage
     */
    override fun onAliasOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance.onAliasOperatorResult(context, jPushMessage)
        super.onAliasOperatorResult(context, jPushMessage)
    }

    /**
     * 设置手机号码会在此方法中回调结果
     *
     * @param context
     * @param jPushMessage
     */
    override fun onMobileNumberOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance.onMobileNumberOperatorResult(context, jPushMessage)
        super.onMobileNumberOperatorResult(context, jPushMessage)
    }

}
